package com.lucy.wx.miniapp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.wx.miniapp.entity.MiniAppGrant;
import com.lucy.wx.miniapp.params.MiniAppGrantCreateParams;
import com.lucy.wx.miniapp.params.MiniAppGrantQueryParams;
import com.lucy.wx.miniapp.params.MiniAppGrantUpdateParams;
import com.lucy.wx.miniapp.service.MiniAppGrantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wx/mini")
@Api(tags = "微信小程序-授权服务")
public class MiniAppGrantController {
    @Autowired
    MiniAppGrantService service;

    @PostMapping("grant")
    @ApiOperation(value = "授权新增",notes = "")
    public ResponseDTO<Object> grantCreate(@RequestBody @Validated MiniAppGrantCreateParams params){
        // 1. 获取APPID
        MiniAppGrant grant = service.getApp(params.getAppId());
        // 2. 如果非空
        if (Validator.isNotEmpty(grant)){
            return ResponseDTO.wrap(HttpStatus.FORBIDDEN,"APPID已存在");
        }
        // 3. 写入
        MiniAppGrant saveEntity = new MiniAppGrant();
        BeanUtil.copyProperties(params,saveEntity);
        MiniAppGrant saveRes = service.saveOne(saveEntity);
        return ResponseDTO.succData(
                saveRes
        );
    }

    @DeleteMapping("grant/{id}")
    @ApiOperation(value = "授权删除",notes = "")
    public ResponseDTO<Object> grantDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("grant")
    @ApiOperation(value = "授权更新",notes = "")
    public ResponseDTO<Object> grantUpdate(@RequestBody @Validated MiniAppGrantUpdateParams params){
        // 1. 获取ID
        String id = params.getId();
        // 2. 获取实体
        MiniAppGrant grant = service.getOneUnwrap(id);
        // 3. 更新写入
        BeanUtil.copyProperties(params,grant);
        return ResponseDTO.succData(
                service.updateOne(grant)
        );
    }

    @GetMapping("grant")
    @ApiOperation(value = "授权列表",notes = "")
    public ResponseDTO<Object> grantListQuery(MiniAppGrantQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }
        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        MiniAppGrant entity = new MiniAppGrant();
        BeanUtil.copyProperties(params,entity, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<MiniAppGrant> example = Example.of(entity);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("grant/{id}")
    @ApiOperation(value = "授权查询",notes = "")
    public ResponseDTO<Object> grantQuery(@PathVariable("id") String id){
        MiniAppGrant grant = service.getOneUnwrap(id);
        return ResponseDTO.succData(
                grant
        );
    }

}
